package ink.ykb.demo.permission.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import ink.ykb.demo.config.CodeConstant;
import ink.ykb.demo.config.MessageUtil;
import ink.ykb.demo.config.ResponseMsg;
import ink.ykb.demo.permission.annotation.Function;
import ink.ykb.demo.permission.entity.Role;
import ink.ykb.demo.permission.entity.Rule;
import ink.ykb.demo.permission.entity.UserRole;
import ink.ykb.demo.permission.service.UserRoleService;

/**
 * 用户用户相关服务
 * 
 * @author mading
 *
 */
@RestController
@RequestMapping("/role/user")
public class UserRoleController {

  @Autowired
  private UserRoleService userRoleService;
  @Autowired
  private MessageUtil messageUtil;

  
  /**
   * 查询用户的所有角色列表
   * 
   * @return
   */
  @Function("admin.role.user.list")
  @GetMapping(value = "/list")
  public ResponseMsg<List<Role>> list(@RequestParam("userId") Long userId) {
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS),
        userRoleService.getUserRoleList(userId));
  }
  
  /**
   * 查询用户的所有权限列表
   * 
   * @return
   */
  @Function("admin.rule.user.list")
  @GetMapping(value = "/rule/list")
  public ResponseMsg<List<Rule>> getRuleSet(@RequestParam("userId") Long userId) {
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS),
        userRoleService.getUserRuleList(userId));
  }
  
  /**
   * 用户添加角色
   * @param userRole 参数：userId,roleId
   * @return
   */
  @Function("admin.role.user.add")
  @PostMapping(value = "/add")
  public ResponseMsg<String> add(UserRole userRole) {
    userRoleService.add(userRole);
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
  }
  
  /**
   * 用户批量添加角色
   * @param userId
   * @param roleIds
   * @return
   */
  @Function("admin.role.user.add.batch")
  @PostMapping(value = "/addBatch")
  public ResponseMsg<String> addBatch(@RequestParam("userId") Long userId,@RequestParam("roleIds") String roleIds) {
    userRoleService.addBatchUserRole(userId, roleIds);
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
  }
  
  /**
   * 用户删除角色
   * @param userId
   * @param roleId
   * @return
   */
  @Function("admin.role.user.detele")
  @PostMapping(value = "/detele")
  public ResponseMsg<String> detele(@RequestParam("userId") Long userId,@RequestParam("roleId") Long roleId) {
    userRoleService.deleteByUserIdAndRoleId(userId, roleId);
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
  }
  
  /**
   * 用户批量删除角色
   * @param userId
   * @param roleIds
   * @return
   */
  @Function("admin.role.user.detele.batch")
  @PostMapping(value = "/deteleBatch")
  public ResponseMsg<String> deteleBatch(@RequestParam("userId") Long userId,@RequestParam("roleIds") String roleIds) {
    userRoleService.deleteBatchUserRole(userId, roleIds);
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
  }
  
  /**
   * 用户批量更新角色
   * @param userId
   * @param roleIds
   * @return
   */
  @Function("admin.role.user.update.batch")
  @PostMapping(value = "/updateBatch")
  public ResponseMsg<String> updateBatch(@RequestParam("userId") Long userId,@RequestParam("roleIds") String roleIds) {
    userRoleService.updateBatchUserRole(userId, roleIds);
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
  }

  
}
